﻿using Infrastructure.Web;
using MyApplication;
using MyData.Dto;
using MyData.Models;
using MyWeb.App_Start;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace MyWeb.Controllers
{
    [HandlerLogin]
    public class MobileController : BaseController
    {

        // GET: Depart
        [HandlerLogin]
        public ActionResult Index()
        {
            return View();
        }
        [HandlerLogin]
        public ActionResult MobileView(string id)
        {
            if (!String.IsNullOrEmpty(id))
            {
                var entity = mobileApp.GetMobile(Guid.Parse(id));
                return View(entity);
            }
            return View(new MobileDto());

        }
        public ActionResult GetMobiles(QueryListModel query, string Name)
        {
            var result = mobileApp.GetMobiles(query, Name);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult SaveMobile(Mobile mobile)
        {
            return base.Try(() =>
            {
                mobileApp.Save(mobile);
            });
        }
        [HttpPost]
        public JsonResult MobileDelete(string Id)
        {
            return base.Try(() =>
            {
                mobileApp.MobileDelete(Guid.Parse(Id));
            });

        }
        [HttpPost]
        public JsonResult MobileReset(string Id)
        {
            return base.Try(() =>
            {
                var mobile = mobileApp.GetMobile(Guid.Parse(Id));
                IPAddress ip = IPAddress.Parse(mobile.Ip);
                IPEndPoint ipe = new IPEndPoint(ip, mobile.Port);//把ip和端口转化为IPEndPoint实例
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
                c.Connect(ipe);//连接到服务器

                byte[] bs = Encoding.UTF8.GetBytes("OK");
                c.Send(bs, bs.Length, 0);//发送测试信息
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
                recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                c.Close();
            });
        }
        [HttpPost]
        public JsonResult MobileResetAll()
        {
            StringBuilder sb = new StringBuilder();
            return base.Try(() =>
            {
                var mobiles = mobileApp.GetMobiles();
                foreach (var m in mobiles)
                {
                    try
                    {
                        IPAddress ip = IPAddress.Parse(m.Ip);
                        IPEndPoint ipe = new IPEndPoint(ip, m.Port);//把ip和端口转化为IPEndPoint实例
                        Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
                        c.Connect(ipe);//连接到服务器

                        byte[] bs = Encoding.UTF8.GetBytes("OK");
                        c.Send(bs, bs.Length, 0);//发送测试信息
                        string recvStr = "";
                        byte[] recvBytes = new byte[1024];
                        int bytes;
                        bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
                        recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                        c.Close();
                    }
                    catch (Exception ex)
                    {
                        sb.AppendLine(m.Name+"同步失败<br/>");
                    }
                }
                if (sb.ToString().Length > 0)
                {
                    throw new KnownException(sb.ToString());
                }
            });
          
        }
    }
}